package cn.com.cetccst.sdk.openapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import cn.com.cetccst.sdk.R;
import cn.com.cetccst.sdk.channel.SSAct;
import cn.com.cetccst.sdk.modelbase.BaseReq;

public abstract class SSBaseEntryActivity extends Activity implements ISSAPIEventHandler {

    private static final String TAG = "CXBaseEntryActivityV2";

    protected ISSAPI api;

    /**
     * 创建CXApi
     */
    protected abstract void createCXApi();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            Intent intent = getIntent();
            createCXApi();
            handleIntent(intent);
        } catch (Exception e) {
            Log.d(TAG, "invalid intent parameter");
        }
        finish();
    }

    private void handleIntent(Intent intent) {
        if (api != null) {
            api.handleIntent(intent, this);
        } else {
            Log.d(TAG, "ssapi is null, please invoke the method createCXAPI");
            int cxSdkVersion = SSAct.getSdkVersion(this, SSAct.getSuitePackage(this));
            int appSdkVersion = SSAct.getSdkVersion(this, getPackageName());
            if (appSdkVersion > cxSdkVersion) {
                Toast.makeText(this, R.string.ss_opensdk_init_failed_higher, Toast.LENGTH_SHORT).show();
            } else if (appSdkVersion > cxSdkVersion) {
                Toast.makeText(this, R.string.ss_opensdk_init_failed_lower, Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(this, R.string.ss_opensdk_init_failed, Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        handleIntent(intent);
        finish();
    }

    public abstract void onReq(BaseReq req);

}
